package com.ruoyi.elebook.service.impl;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.elebook.domain.BookPage;
import com.ruoyi.elebook.domain.BookPageVo;
import com.ruoyi.elebook.mapper.BookPageMapper;
import com.ruoyi.elebook.service.IBookPageService;
import com.ruoyi.system.api.RemoteFileService;
import com.ruoyi.system.api.domain.SysFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 图片页Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-12-11
 */
@Service
public class BookPageServiceImpl implements IBookPageService 
{
    @Autowired
    private BookPageMapper bookPageMapper;
    @Autowired
    private RemoteFileService remoteFileService;

    /**
     * 查询图片页
     * 
     * @param id 图片页主键
     * @return 图片页
     */
    @Override
    public BookPage selectBookPageById(Long id)
    {
        return bookPageMapper.selectBookPageById(id);
    }

    /**
     * 查询图片页列表
     * 
     * @param bookPage 图片页
     * @return 图片页
     */
    @Override
    public List<BookPage> selectBookPageList(BookPage bookPage)
    {

        return bookPageMapper.selectBookPageList(bookPage);
    }

    /**
     * 新增图片页
     * 
     * @param bookPageVo 图片页
     * @return 结果
     */
    @Override
    @Transactional
    public int insertBookPage(BookPageVo bookPageVo)
    {
        R<SysFile> fileResult = remoteFileService.upload(bookPageVo.getFile());
        String url = fileResult.getData().getUrl();
        Long pageSort = bookPageMapper.selectBookPageMaxId(bookPageVo.getElebookId());
        BookPage bookPage = new BookPage();
        bookPage.setPageUrl(url);
        bookPage.setPageSort(pageSort+1);
        bookPage.setPageName(bookPageVo.getFile().getOriginalFilename());
        bookPage.setElebookId(bookPageVo.getElebookId());
        bookPage.setCreateBy(SecurityUtils.getUsername());
        bookPage.setCreateTime(DateUtils.getNowDate());
        return bookPageMapper.insertBookPage(bookPage);
    }

    /**
     * 修改图片页
     * 
     * @param bookPage 图片页
     * @return 结果
     */
    @Override
    public int updateBookPage(BookPage bookPage)
    {
        bookPage.setUpdateTime(DateUtils.getNowDate());
        return bookPageMapper.updateBookPage(bookPage);
    }

    /**
     * 批量删除图片页
     * 
     * @param ids 需要删除的图片页主键
     * @return 结果
     */
    @Override
    public int deleteBookPageByIds(Long[] ids)
    {
        return bookPageMapper.deleteBookPageByIds(ids);
    }

    /**
     * 删除图片页信息
     * 
     * @param id 图片页主键
     * @return 结果
     */
    @Override
    public int deleteBookPageById(Long id)
    {
        return bookPageMapper.deleteBookPageById(id);
    }


    /**
     * 修改图片页首页
     *
     * @param bookPage 图片页
     * @return 结果
     */
    @Override
    @Transactional
    public int updateCover(BookPage bookPage)
    {
        bookPage.setUpdateTime(DateUtils.getNowDate());
        this.bookPageMapper.updateOldCover(bookPage.getElebookId());
        return bookPageMapper.updateCover(bookPage);
    }

}
